#ifndef TCPSERVER_H
#define TCPSERVER_H

#include <qlistview.h>
#include <QListView>
#include <QStringListModel>
#include "tcpclient.h"
#include <QTcpSocket>
#include <QTcpServer>
#include <QString>
#include "threadpool.h"

class TCPServer: public QObject
{
    Q_OBJECT
public:
    explicit TCPServer(QObject *parent = nullptr);
    ~TCPServer();
    void clientRename(QTcpSocket *client, QString rename);
    bool isNumsMax() const
    {
        return m_clients.size() < client_max_nums;
    }
    bool isServerRunning() const
    {
        return m_server->isListening();
    }

signals:
    void showChatMessage(QString message);
    void changeClientNum(qint8 nums);
    void sendServerState(bool m_state);
    void changeClientState(QString thread_id, bool client_state);

public slots:
    void openServer();
    void closeServer();
    void onNewConnection();     //新客户端连接
    void onClientDisconnected();//客户端断开连接

private:
    qint8 client_max_nums;
    qint8 client_nums;
    ThreadPool *threadpool;
    QTcpServer* m_server;           // 负责监听的套接字
    QList<QTcpSocket*> m_clients;   // 所有连接的客户端
    QMutex m_clientsMutex;          // 保护m_clients的互斥锁
};

#endif // TCPSERVER_H
